//
//  DECXRegisterSeatViewController.m
//  DECXInStore
//
//  Created by 李坤 on 2018/9/6.
//  Copyright © 2018年 程强. All rights reserved.
//

#import "DECXRegisterSeatViewController.h"
#import "DECXManager.h"

@interface DECXRegisterSeatViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *personBtn;
@property (weak, nonatomic) IBOutlet UIButton *companyBtn;
@property (weak, nonatomic) IBOutlet UILabel *nameTitleLbl;
@property (weak, nonatomic) IBOutlet UILabel *phoneTitleLbl;
@property (weak, nonatomic) IBOutlet UITextField *nameTxf;
@property (weak, nonatomic) IBOutlet UITextField *phoneTxf;
@property (assign, nonatomic) NSInteger seatType;
@property (strong, nonatomic) DECXManager *manager;
@property (strong, nonatomic) void(^successBlock)(DECXUser *user);
@end

@implementation DECXRegisterSeatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [DECXManager new];
    [self setRightBarItem];
    self.seatType = 2;
}
- (void)setRightBarItem
{
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(commitClick)];
    self.navigationItem.rightBarButtonItem = item;
}
- (void)setSeatType:(NSInteger)seatType
{
    _seatType = seatType;
    if (seatType == 2) {
        _nameTxf.placeholder = @"请填写真实姓名";
        _nameTitleLbl.text = @"姓名";
    }else if (seatType == 1){
        _nameTxf.placeholder = @"请填写公司名称";
        _nameTitleLbl.text = @"公司名称";
    }
}
- (BOOL)checkInput
{
    if (self.nameTxf.text.length == 0) {
        if (_seatType == 2) {
            ToastShowInWindow(@"请填写真实姓名");
        }else{
            ToastShowInWindow(@"请填写公司名称");
        }
        return NO;
    }
    if (![NOValidateHelper validateMobile:self.phoneTxf.text] ) {
        ToastShowInWindow(@"请填写正确的手机号");
        return NO;
    }
    return YES;
}
- (void)onekeyRegister
{
    if (![self checkInput]) {
        return;
    }
    [[self.manager oneKeyRegisterSeatWithName:self.nameTxf.text phone:self.phoneTxf.text type:[NSString stringWithFormat:@"%ld",(long)self.seatType]]subscribeNext:^(id x) {
        DECXUser *user = x;
        if (self.successBlock) {
            self.successBlock(user);
            [self gotoBack];
        }
        ToastShowInWindow(@"注册成功");
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (void)commitClick
{
    weak_Self(self);
    [self resignKeyboard];
    [self performBlock:^{
        [weakSelf onekeyRegister];
    } delay:0.5];
}
- (IBAction)personClick:(id)sender
{
    self.personBtn.selected = YES;
    self.companyBtn.selected = NO;
    self.seatType = 2;
}
- (IBAction)companyClick:(id)sender
{
    self.personBtn.selected = NO;
    self.companyBtn.selected = YES;
    self.seatType = 1;
}
- (void)registerSeatSuccess:(void(^)(DECXUser *user))block
{
    self.successBlock = block;
}
- (void)resignKeyboard
{
    [self.nameTxf resignFirstResponder];
    [self.phoneTxf resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
